package requests

import (
	"gohub/app/http/requests/validators"

	"github.com/gin-gonic/gin"
	"github.com/thedevsaddam/govalidator"
)

type LoginByPhoneRequest struct {
	Phone      string `json:"phone,omitempty" valid:"phone"`
	VerifyCode string `json:"verify_code,omitempty" valid:"verify_code"`
}

type LoginByPasswordRequest struct {
	CaptchaID     string `json:"captcha_id,omitempty" valid:"captcha_id"`
	CaptchaAnswer string `json:"captcha_answer,omitempty" valid:"captcha_answer"`
	LoginID       string `valid:"login_id" json:"login_id"`
	Password      string `json:"password,omitempty" valid:"password"`
}

func LoginByPhone(data interface{}, c *gin.Context) map[string][]string {
	errs := validate(data,
		// 校验规则
		govalidator.MapData{
			"phone":       []string{"required", "digits:11"},
			"verify_code": []string{"required", "digits:6"},
		},
		// 校验错误提示信息
		govalidator.MapData{
			"phone": []string{
				"required:手机号为必填项，参数名称 phone",
				"digits:手机号长度必须为 11 位的数字",
			},
			"verify_code": []string{
				"required:验证码答案必填",
				"digits:验证码长度必须为 6 位的数字",
			},
		},
	)

	// 验证码
	_data := data.(*LoginByPhoneRequest)
	errs = validators.ValidateVerifyCode(_data.Phone, _data.VerifyCode, errs)

	return errs
}

func LoginByPassword(data interface{}, c *gin.Context) map[string][]string {
	errs := validate(data,
		// 校验规则
		govalidator.MapData{
			"captcha_id":     []string{"required"},
			"captcha_answer": []string{"required", "digits:6"},
			"password":       []string{"required", "min:6"},
			"login_id":       []string{"required", "min:3"},
		},
		// 校验错误提示信息
		govalidator.MapData{
			"captcha_id": []string{
				"required:图片验证码的 ID 为必填",
			},
			"captcha_answer": []string{
				"required:图片验证码答案必填",
				"digits:图片验证码长度必须为 6 位的数字",
			},
			"password": []string{
				"required:密码为必填项",
				"min:密码长度需大于 6",
			},
			"login_id": []string{
				"required:登录 ID 为必填项，支持手机号、邮箱和用户名",
				"min:登录 ID 长度需大于 3",
			},
		},
	)

	// 图片验证码
	_data := data.(*LoginByPasswordRequest)
	errs = validators.ValidateCaptcha(_data.CaptchaID, _data.CaptchaAnswer, errs)

	return errs
}
